package com.laicunba.service.accounting.impl;

import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.laicunba.constant.FinancingTargetConstant;
import com.laicunba.pojo.FinancingTargetDO;
import com.laicunba.pojo.RegistryDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.FinancingTargetService;
import com.laicunba.service.RegistryService;
import com.laicunba.service.SaleService;
import com.laicunba.service.UserService;
import com.laicunba.service.AccountService.ACCOUNTING_SUBJECT;
import com.laicunba.service.accounting.AccountingException;
import com.laicunba.service.accounting.AccountingTemplate;
import com.laicunba.service.accounting.RewardBidService;
import com.laicunba.util.AssertUtil;
import com.laicunba.util.StringUtil;

@Service
public class RewardBidServiceImpl extends AccountingTemplate implements RewardBidService {
	@Autowired
	private FinancingTargetService financingTargetService;
	@Autowired
	private SaleService saleService;
	@Autowired
	private UserService userService;
	@Autowired
	private RegistryService registryService;

	@SuppressWarnings("unchecked")
	@Transactional
	@Override
	public boolean doBuy(BuyParam buyParam) {
		// 核对邀请码是否有效
		UserDO userDO = userService.get(UserDO.class, buyParam.getUserId());
		AssertUtil.assertNotNull(userDO, "USER_NOT_EXIST");
		// 找到相应的标的
		FinancingTargetDO targetDO = null;
		if (StringUtil.isNotEmpty(buyParam.getTag())) {
			targetDO = financingTargetService.find(buyParam.getTag(), buyParam.getBuyAmount());
		} else if (StringUtil.isNotEmpty(buyParam.getTargetId())) {
			targetDO = financingTargetService.get(FinancingTargetDO.class, buyParam.getTargetId());
		}
		AssertUtil.assertNotNull(targetDO, "CANT_FIND_TARGET");

		String certificateId = StringUtil.genGUID();

		// 进入购买过程
		SaleService.MakeFinancingTargetBidParam lockParam = new SaleService.MakeFinancingTargetBidParam();
		lockParam.setCertificateId(certificateId);
		lockParam.setFinancingTargetId(targetDO.getId());
		lockParam.setBuyAmount(buyParam.getBuyAmount());
		lockParam.setBuyUserId(userDO.getId());

		if (buyParam.getInviteUser() != null) {
			JSONObject summary = new JSONObject();
			summary.put("到期说明", "推广注册活动,到期回收本金,自动转收益到随存宝");
			summary.put("invite_mobile", buyParam.getInviteUser().getMobile());
			summary.put("invite_name", buyParam.getInviteUser().getTrueName());
			lockParam.setSummary(summary.toJSONString());

		}else{
			lockParam.setSummary("体验金");
		}

		lockParam.setWithdrawType(buyParam.getTag());
		AssertUtil.assertTrue(saleService.makeFinancingTargetBid(lockParam), "LOCK_ERROR");

		SaleService.PayedFinancingTargetParam payedParam = new SaleService.PayedFinancingTargetParam();
		payedParam.setCertificateId(certificateId);
		payedParam.setPayMark("ACTIVITY");
		AssertUtil.assertTrue(saleService.payedFinancingTarget(payedParam), "PAYED_ERROR");

//		StringBuffer totalSignBuffer = new StringBuffer();
//		// 用户借记
//		AssertUtil.assertTrue(theWholeBalanceIsZero(), AccountingException.BALANCE_SHEET_BEFORE_ACCOUNTING_ERROR);
//		String sign = makeUserDr(userDO.getId(), ACCOUNTING_SUBJECT.USER_CURRENCY_TYJ, buyParam.getBuyAmount(), certificateId, "", "");
//		totalSignBuffer.append(sign);
//		AssertUtil.assertNotNull(sign);
//		// 用户贷记
//		sign = makeUserCr(userDO.getId(), ACCOUNTING_SUBJECT.USER_FINANCING_TARGET_BID, buyParam.getBuyAmount(), certificateId, "", "");
//		totalSignBuffer.append(sign);
//		// 平台借记
//		sign = makePlatformDr(ACCOUNTING_SUBJECT.CURRENCY_TYJ, buyParam.getBuyAmount(), certificateId, "", "");
//		totalSignBuffer.append(sign);
//		// 平台贷记
//		sign = makePlatformCr(ACCOUNTING_SUBJECT.FINANCING_TARGET_BID, buyParam.getBuyAmount(), certificateId, "", "");
//		totalSignBuffer.append(sign);
//		AssertUtil.assertTrue(theWholeBalanceIsZero(), AccountingException.BALANCE_SHEET_AFTER_ACCOUNTING_ERROR);

		// 标记注册表
		if (StringUtil.isNotEmpty(buyParam.getRegistryId())) {
			AssertUtil.assertTrue(registryService.create(buyParam.getRegistryId(), "1"));
		}
		return true;
	}
}
